/* newdes.c  -  Revised 3-2-96 to include better key expansion */
/*           -  Released to the public domain by Robert Scott  */
/*           -  Originally published in Cryptologia, Jan. 1985 */

static char f[256] = {
 32,137,239,188,102,125,221,72,212,68,81,37,86,237,147,149,
 70,229,17,124,115,207,33,20,122,143,25,215,51,183,138,142,
 146,211,110,173,1,228,189,14,103,78,162,36,253,167,116,255,
 158,45,185,50,98,168,250,235,54,141,195,247,240,63,148,2,
 224,169,214,180,62,22,117,108,19,172,161,159,160,47,43,171,
 194,175,178,56,196,112,23,220,89,21,164,130,157,8,85,251,
 216,44,94,179,226,38,90,119,40,202,34,206,35,69,231,246,
 29,109,74,71,176,6,60,145,65,13,77,151,12,127,95,199,
 57,101,5,232,150,210,129,24,181,10,121,187,48,193,139,252,
 219,64,88,233,96,128,80,53,191,144,218,11,106,132,155,104,
 91,136,31,42,243,66,126,135,30,26,87,186,182,154,242,123,
 82,166,208,39,152,190,113,205,114,105,225,84,73,163,99,111,
 204,61,200,217,170,15,198,28,192,254,134,234,222,7,236,248,
 201,41,177,156,92,131,67,249,245,184,203,9,241,0,27,46,
 133,174,75,18,93,209,100,120,76,213,16,83,4,107,140,52,
 58,55,3,244,97,197,238,227,118,49,79,230,223,165,153,59};

char B0,B1,B2,B3,B4,B5,B6,B7;
char Key[15];

/* The following code acts on the block B0...B7
   using Key[0]...Key[14] */

encrypt()
{
        int i;
        char ex;
    ex = 0;
    i = 0;
    while(1)
    {
        B4 = B4 ^ f[B0 ^ Key[i] ^ ex];
        if(++i==15) {i = 0;  ex = Key[7];}
        B5 = B5 ^ f[B1 ^ Key[i] ^ ex];
        if(++i==15) {i = 0;  ex = Key[8];}
        B6 = B6 ^ f[B2 ^ Key[i] ^ ex];
        if(++i==15) {i = 0;  ex = Key[9];}
        B7 = B7 ^ f[B3 ^ Key[i] ^ ex];
        if(++i==15) return;

        B1 = B1 ^ f[B4 ^ Key[i++] ^ ex];
        B2 = B2 ^ f[B4 ^ B5];
        B3 = B3 ^ f[B6 ^ Key[i++] ^ ex];
        B0 = B0 ^ f[B7 ^ Key[i++] ^ ex];
      }
}

decrypt()
{
        int i;
        char ex;
    ex = Key[9];
    i = 14;
    while(1)
    {
        B7 = B7 ^ f[B3 ^ Key[i] ^ ex];
        if(--i<0) {i = 14;  ex = Key[8];}
        B6 = B6 ^ f[B2 ^ Key[i] ^ ex];
        if(--i<0) {i = 14;  ex = Key[7];}
        B5 = B5 ^ f[B1 ^ Key[i] ^ ex];
        if(--i<0) {i = 14;  ex = 0;}
        B4 = B4 ^ f[B0 ^ Key[i] ^ ex];

        if(--i<0) return;

        B0 = B0 ^ f[B7 ^ Key[i--] ^ ex];
        B3 = B3 ^ f[B6 ^ Key[i--] ^ ex];
        B2 = B2 ^ f[B4 ^ B5];
        B1 = B1 ^ f[B4 ^ Key[i--] ^ ex];
    }
}
 